package net.sourceforge.simcpux;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.alibaba.fastjson.JSON;
import com.example.vuandroidadsdk.okhttp.CommonOkHttpClient;
import com.example.vuandroidadsdk.okhttp.listener.DisposeDataHandle;
import com.example.vuandroidadsdk.okhttp.listener.DisposeDataListener;
import com.example.vuandroidadsdk.okhttp.request.CommonRequest;
import com.tencent.mm.opensdk.openapi.IWXAPI;

public class MainActivity extends AppCompatActivity {
    IWXAPI msgApi;
    private Button mBtnPay;
   // private String json="{\"appid\":\"wxb4ba3c02aa476ea1\",\"partnerid\":\"1900006771\",\"package\":\"Sign=WXPay\",\"noncestr\":\"d8a61527484c8d76e67560ef8223399b\",\"timestamp\":1540705247,\"prepayid\":\"wx28134047650241157483f2609349808432\",\"sign\":\"A456A0BE31807927C3C73361A486E9C7\"}";
    private String url="https://wxpay.wxutil.com/pub_v2/app/app_pay.php";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {
        mBtnPay = (Button) findViewById(R.id.btn_pay);
        mBtnPay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               getData();
            }
        });
    }

    private void getData(){
        CommonOkHttpClient.get(CommonRequest.createGetRequest(url,null),new DisposeDataHandle(new DisposeDataListener() {
            @Override
            public void onSuccess(Object responseObj) {
                Log.d("wxPayRes",responseObj.toString());
                final WxPayModel model = JSON.parseObject(responseObj.toString(),WxPayModel.class);
                new WXPayUtils.WXPayBuilder()
                        .setAppId(model.getAppid())
                        .setPartnerId(model.getPartnerid())
                        .setPrepayId(model.getPrepayid())
                        .setPackageValue(model.getPackageX())
                        .setNonceStr(model.getNoncestr())
                        .setTimeStamp(model.getTimestamp())
                        .setSign(model.getSign())
                        .build().toWXPayNotSign(MainActivity.this,Constants.APP_ID);
//                msgApi = WXAPIFactory.createWXAPI(MainActivity.this, null);
////
////        // 将该app注册到微信
//                msgApi.registerApp(Constants.APP_ID);
//                Runnable payRunnable = new Runnable() {  //这里注意要放在子线程
//                    @Override
//                    public void run() {
//                        PayReq request = new PayReq(); //调起微信APP的对象
//                        //下面是设置必要的参数，也就是前面说的参数,这几个参数从何而来请看上面说明
//                        request.appId = model.getAppid();
//                        request.partnerId = model.getPartnerid();
//                        request.prepayId = model.getPrepayid();
//                        request.packageValue = model.getPackageX();
//                        request.nonceStr = model.getNoncestr();
//                        request.timeStamp =model.getTimestamp()+"";
//                        request.sign = model.getSign();
//                        msgApi.sendReq(request);//发送调起微信的请求
//                    }
//                };
//                Thread payThread = new Thread(payRunnable);
//                payThread.start();
            }

            @Override
            public void onFailure(Object reasonObj) {

            }
        }));
    }
}
